/***************************************************************************
 *   Copyright (C) 2014 by Philipp Müller                                  *
 *   philipp.mueller@gmx.de                                                *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA          *
 ***************************************************************************/


import QtQuick 2.2
import QtQuick.Controls 1.1
import QtQuick.Dialogs 1.1
import "content"
import QtQuick.Layouts 1.0

ApplicationWindow {
    id: mainWindow
    color: "blue"
    title: "Vomp Remote"
    property int screenReference: 400
    property real screenScale: if ( height < width) { height / screenReference }
                               else { width / screenReference }

    SystemPalette { id: activePalette }

    statusBar: StatusBar {
        id: myStatusBar
        width: parent.width

        RowLayout {
            width: parent.width
            Label {
                text: qsTr( "Send to" ) + ": " + VompRemote.currentHostName
                font.pixelSize: 14 * screenScale
                anchors { right: parent.right; verticalCenter: parent.verticalCenter }
            }
            Label {
                visible: VompRemote.vdrEnabled
                text: {
                    if(  VompRemote.vdrConnected )
                        qsTr( "Connected to" ) + ": " + VompRemote.serverName
                    else
                        qsTr( "Disconnected" )
                }

                font.pixelSize: 14 * screenScale
                anchors { left: parent.left; verticalCenter: parent.verticalCenter }
            }
            anchors { right: parent.right; verticalCenter: parent.verticalCenter }
        }
    }

    toolBar: Rectangle {
        id: myToolBar
        width: mainWindow.width; height: 60 * screenScale
        color: "transparent"

        RowLayout {
            anchors.fill: parent

            ButtonMenu {
                id: buttonVDR
                text: "VDR"
                icon: ""
                visible: VompRemote.vdrConnected
                anchors.left: mainWindow.left

                onClicked: {
                    if ( displayArea.state != "VDR"  )
                        displayArea.state = "VDR"
                }
            } //VDR

            ButtonMenu {
                id: buttonReconnect
                text: "Reconnect"
                icon: ""
                height: buttonPower.height //hack
                visible: !VompRemote.vdrConnected && VompRemote.vdrEnabled

                anchors.left: buttonVDR.right
                onClicked: {
                    VompRemote.vdrConnected = true
                }
            } //Reconnect

            ButtonKey { key: 61;
                        icon:"qrc:/qml/VompRemote/icons/power.png"
                        id: buttonPower
                        anchors.right: parent.right
            } //Power
        }
    }

    Rectangle {
        id: displayArea
        color: "blue"
        anchors.fill: parent
        state: "NORMAL"

        Keys.onReleased: {
//            console.log("Key pressed: " + event.key)
            if ((event.key === Qt.Key_Back ||
                (event.key === Qt.Key_Left && (event.modifiers & Qt.AltModifier)))) {
                console.log("Back button pressed")
                if ( displayArea.state == "OPTIONS" || displayArea.state == "VDR" ) {
                    displayArea.state = "NORMAL"
                    event.accepted = true
                }
            }

            if (event.key == Qt.Key_Menu) {
                console.log("Menu button pressed")
                event.accepted = true
                if ( displayArea.state != "OPTIONS" ) {
                    displayArea.state = "OPTIONS"
                }
            }
        }

        //Messagebox invoked from VompRemote
        MessageDialog {
            id: messageDialog
            visible: false
            title: qsTr( "Information" )
            Component.onCompleted: visible = false
        }

        Item {
            Connections {
                target: VompRemote
                onMessageError: {
                    messageDialog.text = message
                    messageDialog.title = header
                    messageDialog.visible = true
                    console.log( "Message displayed: ", message )
                }
            }
        }

        states: [
            State {
                name: "NORMAL"
                PropertyChanges { target: displayRemote; visible: true }
                PropertyChanges { target: displayVdr; visible: false }
                PropertyChanges { target: myToolBar; visible: true }
                PropertyChanges { target: myStatusBar; visible: true }
                PropertyChanges { target: buttonPower; visible: true }
                PropertyChanges { target: buttonBack; visible: true }
                PropertyChanges { target: areaVolume; visible: true }
                PropertyChanges { target: areaChannels; visible: true }
                PropertyChanges { target: screenOptions; visible: false }
                PropertyChanges { target: screenChannelList; visible: false }
//                PropertyChanges { target: screenTimerList; visible: false }
            },
            State {
                name: "OPTIONS"
                PropertyChanges { target: displayRemote; visible: false }
                PropertyChanges { target: displayVdr; visible: false }
                PropertyChanges { target: myToolBar; visible: false }
                PropertyChanges { target: myStatusBar; visible: false }
                PropertyChanges { target: buttonPower; visible: false }
                PropertyChanges { target: buttonBack; visible: false }
                PropertyChanges { target: areaVolume; visible: false }
                PropertyChanges { target: areaChannels; visible: false }
                PropertyChanges { target: screenOptions; visible: true }
                PropertyChanges { target: screenChannelList; visible: false }
//                PropertyChanges { target: screenTimerList; visible: false }
            },
            State {
                name: "VDR"
                PropertyChanges { target: displayRemote; visible: false }
                PropertyChanges { target: displayVdr; visible: true }
                PropertyChanges { target: myToolBar; visible: false }
                PropertyChanges { target: myStatusBar; visible: false }
                PropertyChanges { target: buttonPower; visible: false }
                PropertyChanges { target: buttonBack; visible: false }
                PropertyChanges { target: areaVolume; visible: false }
                PropertyChanges { target: areaChannels; visible: false }
                PropertyChanges { target: screenOptions; visible: false }
            }
        ]

        VisualItemModel {
            id: displayModelRemote
            Rectangle {
                id: screenCursor
                width: displayArea.width; height: displayArea.height
                color: "blue"
                ScreenCursor {}
            }
            Rectangle {
                id: screenNumber
                width: displayArea.width; height: displayArea.height
                color: "blue"
                ScreenNumber {}
            }
            Rectangle {
                id: screenPlay
                width: displayArea.width; height: displayArea.height
                color: "blue"
                ScreenPlay {}
            }
            Rectangle {
                id: screenMenu
                width: displayArea.width; height: displayArea.height
                color: "blue"
                ScreenMenu {}
            }
        }

        VisualItemModel {
            id: displayModelVdr
            Rectangle {
                id: screenChannelList
                width: displayArea.width; height: displayArea.height
                color: "black"
                ScreenChannelList {}
            }
//            Rectangle {
//                id: screenTimerList
//                width: displayArea.width; height: displayArea.height
//                color: "black"
//                ScreenTimerList {}
//            }
        }

        ListView {
            id: displayRemote
            model: displayModelRemote
            anchors.fill: parent
            snapMode: ListView.SnapOneItem

            focus: true
            orientation: ListView.Horizontal

            move: Transition {
                NumberAnimation { properties: "x,y"; duration: 1000 }
            }
        }

        ListView {
            id: displayVdr
            model: displayModelVdr
            anchors.fill: parent
            snapMode: ListView.SnapOneItem

            focus: true
            orientation: ListView.Horizontal

            move: Transition {
                NumberAnimation { properties: "x,y"; duration: 1000 }
            }
        }

        Rectangle {
            id: screenOptions
            anchors.fill: parent
            visible: false
            ScreenOptions {}
        }

        ButtonKey {
            id: buttonBack
            key: 31
            icon: "qrc:/qml/VompRemote/icons/back.png"
            anchors.left: displayRemote.left; anchors.bottom: displayRemote.bottom
        } //Back

        Rectangle {
            id: areaVolume
            anchors.left: displayRemote.left; anchors.verticalCenter: displayRemote.verticalCenter
            AreaVolume {}
        }

        Rectangle {
            id: areaChannels
            anchors.right: displayRemote.right; anchors.verticalCenter: displayRemote.verticalCenter
            AreaChannelUpDown {}
        }
    }
}
